package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.common.constant.MessageConstant;
import com.atguigu.common.entity.PageResult;
import com.atguigu.common.entity.QueryPageBean;
import com.atguigu.common.entity.Result;
import com.atguigu.common.utils.QiniuUtils;
import com.atguigu.common.utils.RedisConstant;
import com.atguigu.pojo.Setmeal;
import com.atguigu.service.TravelSetmealService;
import com.atguigu.vo.TravelSetmealAddVo;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/travelsetmeal")
public class TravelSetmealController {

    @Reference
    TravelSetmealService travelSetmealService;
    @Autowired
    JedisPool jedisPool;

    //文件上传
    @PostMapping("/upload")
    public Result uploadFile(@RequestParam("imgFile") MultipartFile file){
        //获取文件名称
        String name = file.getOriginalFilename();
        //截取文件
        String substring = name.substring(name.lastIndexOf(".") );
        String fileName = UUID.randomUUID().toString()+substring;
        //获取文件流
        byte[] bytes = new byte[0];
        try {
            bytes = file.getBytes();
            //http://qv5leonqk.hn-bkt.clouddn.com/jjy9456.jpg
            QiniuUtils.upload2Qiniu(bytes,fileName);
            String url = "http://qv5leonqk.hn-bkt.clouddn.com/"+fileName;
            //把文件名称添加到redis中
            Jedis jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,url);
        } catch (IOException e) {
            return new Result(true,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //分页查询
    @PostMapping("/selectPage")
    public Result selectPage(@RequestBody QueryPageBean queryPageBean){
        PageResult result = travelSetmealService.selectPage(queryPageBean.getCurrentPage()
                            ,queryPageBean.getPageSize()
                            ,queryPageBean.getQueryString());
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,result);
    }

    //添加
    @PostMapping("/add")
    public Result setmealAdd(@RequestBody TravelSetmealAddVo setmealAddVo){
        if (setmealAddVo.getSetmeal().getImg()==null){
            return new Result(false,"图片不能为空");
        }
        Jedis jedis = jedisPool.getResource();
        jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmealAddVo.getSetmeal().getImg());
        travelSetmealService.setmealAdd(setmealAddVo);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //查询单个
    @GetMapping("/findById")
    public Result findById(@RequestParam("id")Integer id){
        Setmeal setmeal = travelSetmealService.findById(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    //查询旅游团和套餐关联的id
    @GetMapping("/selectSetmealIdAndGroup")
    public Result selectSetmealIdAndGroup(@RequestParam("id")Integer id){
        List<Integer> ids = travelSetmealService.selectSetmealIdAndGroup(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,ids);
    }
}
